-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add StatusCode enum #1739
Add StatusCode enum #1739
Conversation
I would suggest to make this |
Could you move the change to a separate pull request? I'll merge it right away!
It's a absolutely big breaking change. I understand it's a good practice to enum values instead of hardcoded numbers though, I'll keep the current code as it is since I would not like to break the existing users' code at all. Thanks for your understanding. |
So a plane enum (like in this pr) is okay with you? All the types stays Btw, I checked with our existing code, compiles fine before and after using this enum in Request::status assignments. C++ allows implicit enum to int conversion. |
Here are compile errors which shows the breaking change when compiling 'test/test.cc'. ...
LoopDetected = 508, ///< 508 Loop Detected
NotExtended = 510, ///< 510 Not Extended
NetworkAuthenticationRequired = 511, ///< 511 Network Authentication Required
};
struct Response {
std::string version;
// int status = -1;
StatusCode status = -1;
std::string reason;
Headers headers;
...
|
Even if I added ...
NetworkAuthenticationRequired = 511, ///< 511 Network Authentication Required
NotSet = -1
};
struct Response {
std::string version;
// int status = -1;
StatusCode status = StatusCode::NotSet;
std::string reason;
...
|
@yhirose I meant just an assignment of this enum's values to I'm not suggesting replacing the I don't get any errors here: struct Response {
std::string version;
int status = -1;
std::string reason;
...
response.status = httplib::StatusCode::Forbidden Does this work for you? I believe this should work on every C++ compiler |
@bugdea1er thanks for the clarification. I know understand what you meant. :) Still I am not very comfortable with the enum though. It's because the enum is lacking the category information. The first digit of each status code gives me very valuable information. As soon as I see '2', I can quickly recognize the message is a 'Success' code, and '4' makes me understand it's a request problem. '5' gives me information that something bad happening in the server, and '3' is a redirect code. So if the enum looks like the following, it might be acceptable... enum StatusCode {
...
NotFound_404 = 404,
...
};
int status = StatusCode::NotFound_404; |
@yhirose I added number suffixes to enum constants |
@bugdea1er also we don't need the comments like |
@yhirose ok, I removed them |
@bugdea1er thanks for your quick modification! |
@yhirose I can create a follow-up pr where I replace every number status code with an enum constant in cpp-httplib code Should I? |
@bugdea1er could you make two separate pull requests, the first one is for 'httplib.h' and the other one is for files in 'test' and 'example' directories? Thanks! |
Hi there!
C++ Core Guidelines recommend avoiding "magic constants" (link) and clang-tidy enforces this. I added a enum with Response Status Codes to use them instead of plain numbers in dependent code, please let me know if you would like to use them in httplib too.
Also I changed some of status messages based on RFC 9110Moved RFC 9110 changes to a separate pr #1740